<template>
  <el-dialog 
    :title="dialogTitle" 
    :visible.sync="dialogVisible" 
    width="800px"
    :append-to-body="true"
    @close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="formRef" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="库区编码" prop="stoAreaCode">
            <el-input v-model="form.stoAreaCode" placeholder="请输入库区编码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库区名称" prop="stoAreaName">
            <el-input v-model="form.stoAreaName" placeholder="请输入库区名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库区状态" prop="enableState">
            <el-select v-model="form.enableState" placeholder="请选择状态" style="width: 100%;">
              <el-option label="启用" value="1"></el-option>
              <el-option label="停用" value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="温度要求" prop="temperature">
            <el-input v-model="form.temperature" placeholder="如：常温、阴凉、冷藏" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input 
              v-model="form.remark" 
              type="textarea" 
              :rows="3"
              placeholder="请输入备注信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitLoading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addStoArea, updateStoArea } from "@/api/index/warehouse";

export default {
  name: "AreaDialog",
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogType: 'add',
      dialogVisible: false,
      submitLoading: false,
      form: {
        id: null,
        stoId: null,
        stoAreaCode: '',
        stoAreaName: '',
        enableState: '1',
        temperature: '',
        remark: ''
      },
      rules: {
        stoAreaCode: [
          { required: true, message: '请输入库区编码', trigger: 'blur' }
        ],
        stoAreaName: [
          { required: true, message: '请输入库区名称', trigger: 'blur' }
        ],
        enableState: [
          { required: true, message: '请选择库区状态', trigger: 'change' }
        ]
      }
    };
  },
  computed: {
    dialogTitle() {
      return this.dialogType === 'add' ? '新增库区' : '编辑库区';
    }
  },
  watch: {
    visible(val) {
      this.dialogVisible = val;
    },
    dialogVisible(val) {
      this.$emit('update:visible', val);
    }
  },
  methods: {
    openDialog(type, data, stoId) {
      this.dialogType = type;
      this.dialogVisible = true;
      
      console.log('打开库区对话框:', type, data);
      
      if (type === 'edit' && data) {
        this.form = {
          id: data.stoAreaId || data.id,
          stoId: data.stoId || stoId,
          stoAreaCode: data.stoAreaCode,
          stoAreaName: data.stoAreaName,
          enableState: String(data.enableState || '1'),
          temperature: data.temperature || '',
          remark: data.remark || ''
        };
        console.log('编辑表单数据:', this.form);
      } else {
        this.form = {
          id: null,
          stoId: stoId,
          stoAreaCode: '',
          stoAreaName: '',
          enableState: '1',
          temperature: '',
          remark: ''
        };
      }
    },
    
    handleClose() {
      this.dialogVisible = false;
      this.$refs.formRef.resetFields();
    },
    
    handleSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          const apiMethod = this.dialogType === 'add' ? addStoArea : updateStoArea;
          
          apiMethod(this.form).then(res => {
            this.$message.success(res.msg || '操作成功');
            this.submitLoading = false;
            this.dialogVisible = false;
            this.$emit('success');
          }).catch(err => {
            this.$message.error(err.msg || '操作失败');
            this.submitLoading = false;
          });
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.dialog-footer {
  text-align: right;
}
</style>
